package com.bawei.rk_01.address

import android.os.Bundle
import android.widget.CheckBox
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.utils.mvvm.BaseAcitivity
import com.bawei.rk_01.Adapter.AddressAdapter
import com.bawei.rk_01.R
import com.bawei.rk_01.databinding.ActivityAddressBinding
import com.blankj.utilcode.util.ToastUtils

@Route(path = "/app/AddressActivity")

class AddressActivity : BaseAcitivity<ActivityAddressBinding,AddressViewModel>() {

    lateinit var addressAdapter: AddressAdapter
    var index = -1

    override fun getViewModel(): Class<AddressViewModel> {
        return AddressViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.activity_address
    }

    override fun initView() {

        //点击跳转
        binding.addAddress.setOnClickListener {
            ARouter.getInstance().build("/app/AddADdressActivity").navigation()
        }

        addressAdapter = AddressAdapter()
        viewModel.list(20,1)
        binding.rvAddress.layoutManager = LinearLayoutManager(this)
        binding.rvAddress.adapter = addressAdapter

        //列表子布局点击事件
        addressAdapter.addChildClickViewIds(R.id.cb1,R.id.del)
        addressAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.cb1->{
                    index = position
                    viewModel.moren(addressAdapter.data.get(position).id)
                    val checkBox = addressAdapter.getViewByPosition(position, R.id.cb1) as CheckBox
                    checkBox.isChecked = true
                }
                R.id.del->{
                    viewModel.deladdress(addressAdapter.data.get(position).id)
                }
            }
        }

    }

    override fun initData() {

        viewModel.listsuccess.observe(this){
            if (it.code == 200){
                addressAdapter.data.clear()
                addressAdapter.data.addAll(it.data.list)
                addressAdapter.notifyDataSetChanged()
            }else{
                ToastUtils.showLong(it.message)
            }
        }

        viewModel.morensuccess.observe(this){
            if (it.code == 200){
                ToastUtils.showLong(it.message)
                val checkBox = addressAdapter.getViewByPosition(index, R.id.cb1) as CheckBox
                checkBox.isChecked = true
            }else{
                ToastUtils.showLong(it.message)
                val checkBox = addressAdapter.getViewByPosition(index, R.id.cb1) as CheckBox
                checkBox.isChecked = false
            }
        }

        viewModel.delsuccess.observe(this){
            if (it.code == 200){
                ToastUtils.showLong("删除成功")
                viewModel.list(20,1)
            }else{
                ToastUtils.showLong(it.message)
            }
        }

    }

}